package com.zht.utils;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

/**
 * 豫事办用加密解密工具类
 */
public class STAESUtil {

    /**
     * AES加密
     *
     * @param data 要加密的字符串
     * @param key  加密key
     * @param iv   密码加密算法中的IV
     * @return 加密后的字符串
     */
    public static String encrypt(String data, String key, String iv) {
        try {

            BASE64Encoder encoder = new BASE64Encoder();
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES");
            IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes());
            cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
            byte[] encrypted = cipher.doFinal(data.getBytes("utf-8"));
            return encoder.encode(encrypted);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }


    /**
     * AES解密
     *
     * @param data 加密后的字符串
     * @param key  加密key
     * @param iv   密码加密算法中的IV
     * @return 解密后的字符串
     */
    public static String decrypt(String data, String key, String iv) {
        try {

            BASE64Decoder decode = new BASE64Decoder();
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES");
            IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes());
            cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);
            byte[] decrypted = decode.decodeBuffer(data);
            byte[] original = cipher.doFinal(decrypted);
            return new String(original, "utf-8");
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static void main(String[] args) throws UnsupportedEncodingException {
        //豫事办单证
//        String data = "徐拴霞&410105199305100104&13253619481&"+System.currentTimeMillis();
//
//        String ysb = AESUtil.encrypt(data, "fbaa241540!@#$%^", "asdfghjklzxcvbnm");
//        System.out.println("加密后："+ysb);
//        String ysbjm = AESUtil.decrypt(ysb, "fbaa241540!@#$%^", "asdfghjklzxcvbnm");
//        System.out.println("解密后："+ysbjm);

        // 河南警民通
//        String hlw = AESUtil.encrypt(data, "qwertyuiop!@#$%^", "asdfghjklzxcvbnm");
//        System.out.println("加密后："+hlw);
//        String hlwjm = AESUtil.decrypt(hlw, "qwertyuiop!@#$%^", "asdfghjklzxcvbnm");
//        System.out.println("解密后："+hlwjm);

        // 郑州警民通
//        String hlw = AESUtil.encrypt(data, "27d6b49a38!@#$%^", "asdfghjklzxcvbnm");
//        System.out.println("加密后："+hlw);
//        String hlwjm = AESUtil.decrypt(hlw, "27d6b49a38!@#$%^", "asdfghjklzxcvbnm");
//        System.out.println("解密后："+hlwjm);

        // 重置密码
//        PlusCut plusCut = PlusCut.getInstance();
//        String plus = plusCut.plus("03106274");
//        System.out.println(plus);


        // 工商银行
        String data = "ICBC&hngadzzz&"+System.currentTimeMillis();
        String hlw = STAESUtil.encrypt(data, "tyuqweop17!@#$%^", "asdfghjklzxcvbnm");
        System.out.println("加密后："+hlw);
        String hlwjm = STAESUtil.decrypt(hlw, "tyuqweop17!@#$%^", "asdfghjklzxcvbnm");
        System.out.println("解密后："+hlwjm);
        String encode = URLEncoder.encode(hlw, "UTF-8");
        System.out.println(encode);
    }

}
